<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <!-- 引入 Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 自定义 CSS -->
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Arial', sans-serif;
        }
        .login-container {
            max-width: 500px;
            margin: 100px auto;
            padding: 30px;
            background: #ffffff;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
            font-weight: bold;
        }
        .form-control {
            border-radius: 5px;
            padding: 10px;
            border: 1px solid #ddd;
            margin-bottom: 15px;
        }
        .form-control:focus {
            border-color: #007bff;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.25);
        }
        .btn-primary {
            width: 48%;
            padding: 10px;
            border-radius: 5px;
            background-color: #007bff;
            border: none;
            font-size: 16px;
        }
        .btn-primary :hover {
            background-color: #0056b3;
        }
        .alert {
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .text-danger {
            font-size: 14px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>登录</h2>
        <form method="post" action="{% url 'login' %}">
            {% csrf_token %}  <!-- CSRF 令牌，防止跨站请求伪造攻击 -->

            <!-- 显示表单错误 -->
            {% if form.errors %}
                <div class="alert alert-danger">
                    <strong>登录异常：{{ form.errors }}</strong>
                </div>
            {% endif %}

            <!-- Username 字段 -->
            <div class="form-group">
                <label for="{{ form.username.id_for_label }}" class="form-label">用户名</label>
                <input type="text" name="username" id="id_username" class="form-control" value="{{ form.username.value|default_if_none:'' }}" required>
                {% if form.username.errors %}
                    <div class="text-danger">
                        {{ form.username.errors }}
                    </div>
                {% endif %}
            </div>

            <!-- Password 字段 -->
            <div class="form-group">
                <label for="{{ form.password.id_for_label }}" class="form-label">密码</label>
                <input type="text" name="password" id="id_password" class="form-control" required>
                {% if form.password.errors %}
                    <div class="text-danger">
                        {{ form.password.errors }}
                    </div>
                {% endif %}
            </div>

            <!-- 提交按钮 -->
            <div class="form-group">
                <button type="button" onclick="window.location.href='{% url 'main' %}'" class="btn btn-primary" >返回首页</button>
                <button type="submit" class="btn btn-primary">登录</button>
            </div>
        </form>
    </div>

    <!-- 引入 Bootstrap 5 JS（可选） -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
